﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASCalpuff.FileLayers
{
    public class PG3:Group
    {
        public PG3()
        {
            m_Ps = new List<PollutionSpecies>();
            m_Ps.Add(new PollutionSpecies("SO2", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("NOX", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("HNO3", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("NO", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("NO2", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("ODOR", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("TOLUENE", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("XYLENE", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("B-PINENE", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("A-PINENE", true, true, 1, 0));
            m_Ps.Add(new PollutionSpecies("SO4", true, true, 2, 0));
            m_Ps.Add(new PollutionSpecies("NO3", true, true, 2, 0));
            m_Ps.Add(new PollutionSpecies("PM10", true, true, 2, 0));
            m_Ps.Add(new PollutionSpecies("SOA", true, true, 2, 0));
            m_Ps.Add(new PollutionSpecies("H2O", true, true, 2, 0));
            m_Ps.Add(new PollutionSpecies("TXS", true, true, 2, 0));
            m_Ps[0].CalculateSpecies = true;
        }


        //包括所污染物名称、模拟时用的标记物种及其平流输送、扩散、干沉淀、湿去除等过程
        //Group 3由两部分组成，第一部分包含一个污染物列表及每种污染物的四个标志，
        //这些标志用来表示，是否预测污染物，污染物是否有排放，
        //是否考虑污染物干沉积（=0，不考虑；=1，气体沉积；=2，颗粒物沉积，=3用户定义的沉积速度）。
        //第二部分列出污染物名称。


        //用户首先必须列出污染物的名字，每种写在一个单独行内如下列格式
        //！CSPEC= XXX ！ ！END！  XXX指的是污染物的名字（最多12个字符）
        //界定符必须和污染物写在同一行内。如下列 预测5中污染物并采用MCHEM=1。


        /// <summary>
        /// 种类的集合
        /// </summary>
        List<PollutionSpecies> m_Ps;

        public List<PollutionSpecies> Ps
        {
            get { return m_Ps; }
            set { m_Ps = value; }
        }


        public override string WriteToGroup()
        {
            string Subgroup = "", TempStr;

            TempStr = StrWriter.GetCLine();
            TempStr += "INPUT GROUP: 3a, 3b -- Species list" + "\r\n";
            Subgroup += TempStr;

            TempStr = StrWriter.GetCLine();
            Subgroup += TempStr;

            //进行排序
            List<string> arry = Sort.GetPollutantRightOrder();
            List<PollutionSpecies> arryPollut = new List<PollutionSpecies>();
            arryPollut.Clear();
            foreach (string a in arry)
            {
                for (int i = 0; i < Ps.Count; i++)
                {
                    if (a == Ps[i].Name)
                    {
                        arryPollut.Add(Ps[i]);
                    }
                }
            }


            //＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝
            if (arryPollut.Count > 0)
            {
                for (int i = 0; i < arryPollut.Count; i++)
                {
                    if (arryPollut[i].CalculateSpecies)
                    {
                        TempStr = "!  CSPEC =   " + arryPollut[i].Name + "   ! " + "         !END!" + "\r\n";
                        Subgroup += TempStr;
                    }
                }
                for (int i = 0; i < arryPollut.Count; i++)
                {
                    if (arryPollut[i].CalculateSpecies)
                    {
                        TempStr = "!    " + arryPollut[i].Name + "  =  ";
                        if (arryPollut[i].BModeled == true)
                        {
                            TempStr += "   " + "1" + ",    ";
                        }
                        else
                        {
                            TempStr += "   " + "0" + ",    ";
                        }
                        if (arryPollut[i].BEmitted == true)
                        {
                            TempStr += "   " + "1" + ",    ";
                        }
                        else
                        {
                            TempStr += "   " + "0" + ",    ";
                        }

                        TempStr += "   " + arryPollut[i].DryDeposited.ToString() + ",   " + arryPollut[i].OutputGroupNumber.ToString() + "   ! " + "\r\n";
                        Subgroup += TempStr;
                    }
                }
                Subgroup += "!END!" + "\r\n";
            }

            return Subgroup;
        }
        //Note:  The last species in (3a) must be 'BCON' when using the
        // boundary condition option (MBCON > 0).  Species BCON should
        // typically be modeled as inert (no chem transformation or
        // removal).


        //      The following names are used for Species-Groups in which results
        //for certain species are combined (added) prior to output.  The
        //CGRUP name will be used as the species name in output files.
        //Use this feature to model specific particle-size distributions
        //by treating each size-range as a separate species.
        //Order must be consistent with 3(a) above.

        //在这部分中，污染物组名字设定为“PM10”。
        //因为在group3a中 output group number只有1，所以在此只能设定一个污染物分组名称。
        //在group5中，分组名称在选择输出选项时用到，并且这个名称写到输出文件里面（后处理程序用到）。
        //	没有分组污染物保持其原有的名称。
        //	Group3b中列出的污染物分组的顺序必须和group3a中一致


        public override void ReadFromGroup(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> m_dic in dic)
            {
                if (m_dic.Key != "CSPEC")
                {
                    PollutionSpecies po = new PollutionSpecies("SO2", true, true, 1, 0);

                    this.Ps.Add(po.ReadFromGroup(m_dic.Key, m_dic.Value));

                }
            }

        }


    }


    public class PollutionSpecies : ICloneable
    {
        public PollutionSpecies(string name, bool modeled, bool emitted, uint dryDep, uint groupnum)
        {
            m_Name = name;

            m_bModeled = modeled;

            m_bEmitted = emitted;

            m_DryDeposited = dryDep;

            m_OutputGroupNumber = groupnum;

        }
        public PollutionSpecies ReadFromGroup(string name, string str)
        {
            PollutionSpecies ps = new PollutionSpecies("SO2", true, true, 1, 0);

            ps.m_Name = name;
            if (StrWriter.GetString(str, 1) == "1")
            {
                ps.m_bModeled = true;
            }
            else
            {
                ps.m_bModeled = false;
            }


            if (StrWriter.GetString(str, 2) == "1")
            {
                ps.m_bEmitted = true;
            }
            else
            {
                ps.m_bEmitted = false;
            }
            ps.m_DryDeposited = Convert.ToUInt32(StrWriter.GetString(str, 3));
            ps.m_OutputGroupNumber = Convert.ToUInt32(StrWriter.GetString(str, 4));

            return ps;
        }
        //少于12个字符的污染源名称

        string m_Name;

        public string Name
        {
            get { return m_Name; }
            set { m_Name = value; }
        }

        bool m_bModeled;

        public bool BModeled
        {
            get { return m_bModeled; }
            set { m_bModeled = value; }
        }

        bool m_bEmitted;

        public bool BEmitted
        {
            get { return m_bEmitted; }
            set { m_bEmitted = value; }
        }

        uint m_DryDeposited;

        public uint DryDeposited
        {
            get { return m_DryDeposited; }
            set { m_DryDeposited = value; }
        }

        uint m_OutputGroupNumber;

        public uint OutputGroupNumber
        {
            get { return m_OutputGroupNumber; }
            set { m_OutputGroupNumber = value; }
        }
        //private bool m_WETDeposited = false;

        //public bool WETDeposited
        //{
        //    get { return m_WETDeposited; }
        //    set { m_WETDeposited = value; }
        //}

        private bool m_CalculateSpecies = false;
        /// <summary>
        /// 是否估算污染物
        /// </summary>
        public bool CalculateSpecies
        {
            get { return m_CalculateSpecies; }
            set { m_CalculateSpecies = value; }
        }

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }


}
